package com.teemor.tim.contoller;

import com.alibaba.fastjson.JSON;
import com.teemor.common.bean.utils.tmpl.BaseController;
import com.teemor.common.bean.utils.tmpl.ResponseBen;
import com.teemor.tim.TencentIMHelper;
import com.teemor.tim.bean.form.openim.ImportMsgForm;
import com.teemor.tim.bean.form.openim.SendMsgForm;
import com.teemor.tim.bean.form.sns.FriendAddForm;
import com.teemor.tim.bean.form.CreateGroupForm;
import com.teemor.tim.command.model.BaseCommand;
import com.teemor.tim.command.model.CommandParam;
import com.teemor.tim.bean.form.sns.FriendCheckForm;
import com.teemor.tim.bean.form.sns.FriendDeleteForm;
import com.teemor.tim.bean.result.callback.CallBackResult;
import com.teemor.tim.bean.result.openim.ImportMsgResult;
import com.teemor.tim.bean.result.openim.SendMsgResult;
import com.teemor.tim.bean.result.sns.FriendCheckResult;
import com.teemor.tim.bean.result.sns.FriendDeleteResult;
import com.teemor.tim.command.CommandExecute;
import com.teemor.tim.model.group.GroupAddModel;
import com.teemor.tim.bean.result.sns.FriendAddResult;
import com.teemor.tim.model.message.TimMessage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collections;
import java.util.Random;

/**
 * @ename zhoulk
 * @cname 周李科
 * @date 2020/12/9 14:49
 */
@RequestMapping("/tim")
@RestController
@Slf4j
@Api(tags = "腾讯IM")
public class TimController extends BaseController {

    @Autowired
    private CommandExecute commandExecute;

    @Autowired
    private TencentIMHelper tencentIMHelper;

    @ApiOperation("回调接口")
    @PostMapping(value = "/callback")
    public CallBackResult callback(@ModelAttribute CommandParam commandParam, @RequestBody String form) {
        BaseCommand baseCommand = JSON.parseObject(form, BaseCommand.class);
        log.info("【tim_callback】 commandParam: {}, CallbackCommand: {}", JSON.toJSONString(commandParam), baseCommand.getCallbackCommand());
        CallBackResult result = commandExecute.execute(commandParam, baseCommand, form);
        return result;
    }

    @PostMapping(value = "/friendAdd")
    @ApiOperation("添加好友")
    public ResponseBen<FriendAddResult> addFriend(@RequestBody FriendAddForm form) {
        FriendAddResult result = tencentIMHelper.friendAdd(form);
        return returnSuccess(result);
    }


    @PostMapping(value = "friendDelete")
    @ApiOperation("删除好友")
    public ResponseBen<FriendDeleteResult> friendDelete(@RequestBody FriendDeleteForm form) {
        FriendDeleteResult result = tencentIMHelper.friendDelete(form);
        return returnSuccess(result);
    }


    @PostMapping(value = "friendCheck")
    @ApiOperation("校验好友")
    public ResponseBen<FriendCheckResult> friendCheck(@RequestBody FriendCheckForm form) {
        FriendCheckResult result = tencentIMHelper.friendCheck(form);
        return returnSuccess(result);
    }

    @PostMapping(value = "createGroup")
    @ApiOperation("创建分组")
    public ResponseBen<GroupAddModel> test(@RequestBody CreateGroupForm from) {
        log.info(JSON.toJSONString(from));
        GroupAddModel result = tencentIMHelper.createGroup(from);
        return returnSuccess(result);
    }

    @PostMapping(value = "import")
    @ApiOperation("导入消息")
    public ResponseBen<ImportMsgResult> importMsg(@RequestBody ImportMsgForm form) {
        form.setMsgRandom(new Random().nextInt(8999) + 1000);
        log.info(JSON.toJSONString(form));
        ImportMsgResult result = tencentIMHelper.importMsg(form);
        return returnSuccess(result);
    }

    @PostMapping(value = "sendmsg")
    @ApiOperation("发送单聊消息")
    public ResponseBen<SendMsgResult> sendMsg(String fromAccount, @RequestParam String toAccount, @RequestBody TimMessage form) {

        SendMsgForm sendMsgForm = new SendMsgForm();
        sendMsgForm.setFromAccount(fromAccount);
        sendMsgForm.setToAccount(toAccount);
        sendMsgForm.setMsgRandom(new Random().nextInt(8999) + 1000);
        sendMsgForm.setMsgTimeStamp(Long.valueOf(System.currentTimeMillis() / 1000).intValue());
        sendMsgForm.setMsgBody(Collections.singletonList(form));
        SendMsgResult result = tencentIMHelper.sendMsg(sendMsgForm);
        return returnSuccess(result);
    }

}
